package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @Author Andy
 * @Description //TODO EmployeeController
 * @Date 2021/10/26 7:13 下午
 * @Version 1.0
 **/
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        /*
        1、根据页面提交的用户名username查询数据库
2、如果没有查询到则返回登录失败结果
3、将页面提交的密码password进行md5加密处理
4、密码比对，如果不一致则返回登录失败结果
5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
6、登录成功，将员工id存入Session并返回登录成功结果

         */

        Employee one = employeeService.getOne(new LambdaQueryWrapper<Employee>()
                                                      .eq(Employee::getUsername,
                                                          employee.getUsername()));
        if (Objects.isNull(one)){
            return R.error("登录失败");
        }
        if (!StringUtils.equals(one.getPassword(), DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()))){
            return R.error("登录失败");
        }
        if (one.getStatus() == 0){
            return R.error("账号已经禁用");
        }
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);
    }

    @RequestMapping("/loginout")
    public R<String> loginout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("登出成功");
    }

    @PostMapping
    public R<Employee> save(HttpServletRequest request, @RequestBody Employee employee){
        LocalDateTime now = LocalDateTime.now();
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(now);
        employee.setUpdateTime(now);
        employee.setCreateUser(Long.valueOf(request.getSession().getAttribute("employee").toString()));
        employee.setUpdateUser(Long.valueOf(request.getSession().getAttribute("employee").toString()));
        employeeService.save(employee);
        return R.success(employee);
    }

    @PutMapping
    public R<Employee> update(HttpServletRequest request, @RequestBody Employee employee){
        LocalDateTime now = LocalDateTime.now();
        employee.setUpdateTime(now);
        employee.setUpdateUser(Long.valueOf(request.getSession().getAttribute("employee").toString()));
        employeeService.updateById(employee);
        return R.success(employee);
    }

    @RequestMapping("/page")
    public R<Page<Employee>> page(int page, int pageSize, String name){

        Page<Employee> employeePage = new Page<>();
        employeePage.setPages(page);
        employeePage.setSize(pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), Employee::getUsername, name);

        Page<Employee> pageResult = employeeService.page(employeePage, queryWrapper);

        return R.success(pageResult);
    }

    @GetMapping("/{id}")
    public R<Employee> update(HttpServletRequest request, @PathVariable("id") Long id){
        return R.success(employeeService.getById(id));
    }


}
